<?php
class item_galleryavatar extends template_item
{
	var $default_name = "Upload A Gallery Avatar";
	var $default_description = "Upload a gallery avatar for people to use for their avatar.";
	
	var $allow_multiple = 1;
	var $can_run_multiple = 1;
	
	var $gallery_dir = "";
	
	function load_item(&$item)
	{
		$this->gallery_dir = CACHE_PATH.'style_avatars/IBMarket_Avatars';
		
		parent::load_item(&$item);
	}
	
	function admin_save()
	{
		if( is_dir($this->gallery_dir) )
		{
			if( !is_writeable($this->gallery_dir) && chmod($this->gallery_dir, 0777) )
			{
				$this->ipsclass->main_msg = "Folder 'IBMarket_Avatars' made writeable inside style_avatars.";
			}
			else
			{
				$this->ipsclass->main_msg = "Folder 'IBMarket_Avatars' inside style_avatars is ready to go.";
			}
		}
		else
		{
			if( is_writeable(CACHE_PATH.'style_avatars') && mkdir($this->gallery_dir, 0777) )
			{
				$this->ipsclass->main_msg = "Folder 'IBMarket_Avatars' made inside style_avatars.";
			}
			else
			{
				$this->ipsclass->main_msg = "Please make a folder called 'IBMarket_Avatars' inside style_avatars. CHMOD IBMarket_Avatars to 0777.";
			}
		}
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_ucp');
		$this->ipsclass->load_language('lang_market_items');
		
		if(!is_dir($this->gallery_dir))
		{
			$this->ipsclass->Error(array('MSG'=>'gallery_avatar_dir_error'));
		}
		if(!is_writeable($this->gallery_dir))
		{
			$this->ipsclass->Error(array('MSG'=>'gallery_avatar_dir_error'));
		}
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
 		list( $this->ipsclass->vars['av_width'], $this->ipsclass->vars['av_height']) = explode ("x", $this->ipsclass->vars['avatar_dims']);
 		$this->ipsclass->lang['av_allowed_files'] = sprintf($this->ipsclass->lang['av_allowed_files'], implode (' .', explode( "|", $this->ipsclass->vars['avatar_ext'] ) ) );
		$this->ipsclass->lang['av_text_url'] = sprintf( $this->ipsclass->lang['av_text_url'], $this->ipsclass->vars['av_width'], $this->ipsclass->vars['av_height'] );
		$this->ipsclass->lang['av_text_upload'] = sprintf( $this->ipsclass->lang['av_text_upload'], $this->ipsclass->vars['avup_size_max'] );
 		
 		if ( $this->ipsclass->vars['allow_flash'] != 1 )
		{
			$this->ipsclass->lang['av_allowed_files'] = str_replace( ".swf", "", $this->ipsclass->lang['av_allowed_files'] );
		}
		
		$upload_form = "";
		foreach($inventory_ids as $inv_id)
		{
			$upload_form .= <<<EOF
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['select_an_image_file']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='file' size='30' name='upload_avatar_{$inv_id}' />
			</td>
		</tr>
EOF;
		}
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['upload_gallery_avatar']}</div>
<form action='{$this->base_url}' method='post' enctype='multipart/form-data'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left' colspan='2'>
				{$this->ipsclass->lang['av_text_url']} {$this->ipsclass->lang['av_text_upload']}
				<br />
				{$this->ipsclass->lang['av_allowed_files']}
			</td>
		</tr>
{$upload_form}
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['upload_avatars']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done($inventory_ids = array())
	{
		foreach($inventory_ids as $inv_id)
		{
			if(!isset($_FILES['upload_avatar_'.$inv_id]) || !is_uploaded_file($_FILES['upload_avatar_'.$inv_id]['tmp_name']))
			{
				return false;
			}
		}
		return isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		if(!is_dir($this->gallery_dir))
		{
			$this->ipsclass->Error(array('MSG'=>'gallery_avatar_dir_error'));
		}
		if(!is_writeable($this->gallery_dir))
		{
			$this->ipsclass->Error(array('MSG'=>'gallery_avatar_dir_error'));
		}
		
		require_once(KERNEL_PATH."class_upload.php");
		$upload = new class_upload();
		
		$delete = $inventory_ids;
		
		$exts = array();
		foreach(explode( ",", $this->ipsclass->vars['avatar_ext'] ) as $ext)
		{
			$exts[] = trim($ext);
		}
		
		foreach($inventory_ids as $i => $inv_id)
		{
			$upload->error_no = 0;
			$upload->upload_form_field = 'upload_avatar_'.$inv_id;
			$upload->out_file_dir = $this->gallery_dir;
			$upload->max_file_size = ($this->ipsclass->vars['avup_size_max'] * 1024) * 8;
			$upload->allowed_file_ext = $exts;
			if($_FILES['upload_avatar_'.$inv_id]['name'] && $_FILES['upload_avatar_'.$inv_id]['name']!= 'none' && file_exists($this->gallery_dir."/".$_FILES['upload_avatar_'.$inv_id]['name']))
			{
				$upload->out_file_name = str_replace( '.'.$upload->_get_file_extension($_FILES['upload_avatar_'.$inv_id]['name']), "", $_FILES['upload_avatar_'.$inv_id]['name'] )."-".time();
			}
			else
			{
				$upload->out_file_name = "";
			}
			
			$upload->upload_process();
			
			if($upload->error_no)
			{
				unset($delete[$i]);
			}
			
			$this->_write_log($inv_id, "Uploaded gallery avatar, {$upload->parsed_file_name}");
		}
		
		$this->delete_items($delete);
		
		$this->ipsclass->market_class->redirect('gallery_avatars_uploaded',$this->ipsclass->base_url.'act=UserCP&CODE=getgallery&av_cat=IBMarket_Avatars',array('url'=>1));
	}
}
?>